<template>
   <div class="container">
     <div class="inpbox">
          <x-input title="收件人" label-width="170px" name="username" v-model="adresslist.personName" placeholder="请输入收件人姓名" is-type="china-name"></x-input>
          <x-input title="手机号" label-width="170px" name="mobile" v-model="adresslist.phone" placeholder="请输入手机号码" keyboard="number" is-type="china-mobile"></x-input>
          <x-address  title="省市区" label-width="170px" v-model="areaCodetext" raw-value :list="addressData"  placeholder="请选择地址"  value-text-align="left"  ></x-address>
          <x-input title="详细地址" label-width="170px" name="text" v-model="adresslist.address" placeholder="请输入详细地址" ></x-input>
          <x-switch title="设为默认地址" :value-map="[0, 1]"  v-model="adresslist.status"></x-switch>
     </div>
     <div class="address-bom" @click="submit">
       保存
     </div>
   </div>
</template>
<script>
import { XInput,XAddress, XSwitch,ChinaAddressV4Data} from 'vux'

export default {
   components:{
       XInput,
       XAddress,
       XSwitch
   },
   data () {
	    return {
         addressData: ChinaAddressV4Data,
         showAddress: false,
         areaCodetext:[],
          adresslist:{
              phone:'',
              personName:'',
              areaCode:[],
              address:'',
              status:'',
              userId:this.$route.query.userId
           }
	     }
     },
      created(){
      this.getadresslist()
    },
     methods:{
     getadresslist(){
        if(this.$route.query.adressId){    //获取地址
           var self=this
          this.$axios.post('/device/userAddress/get',{id:this.$route.query.adressId}).then(res=>{
            if(res.data.code==1000){
                  console.log(res)
                  self.areaCodetext=res.data.data.areaCodeList
                  self.adresslist={
                    id:res.data.data.id,
                    phone:res.data.data.phone,
                    personName:res.data.data.name,
                    address:res.data.data.address,
                    status:res.data.data.status,
                    userId:this.$route.query.userId
                  }
            }
           })
        }
      },
      submit(){
          this.adresslist.areaCode=this.areaCodetext[2]
          console.log(this.adresslist)
          var self=this
          if(this.$route.query.orderNumber){
             this.$router.push({path:"/SureOrder",query:{ userId:self.$route.query.userId,orderNumber:self.$route.query.oldshopNumber }})
          }else{
          if(this.$route.query.adressId){
              this.adresslist.id=this.$route.query.adressId

             this.$axios.post('/device/userAddress/update/',this.adresslist).then(res=>{
            if(res.data.code==1000){
               this.$vux.toast.text('修改地址成功')
               self.$router.push({ path:'/Adress', query: { userId: self.$route.query.userId }})
            }
           })
          }else{
             this.$axios.post('/device/userAddress/create',this.adresslist).then(res=>{
            if(res.data.code==1000){
               self.$router.push({ path: '/Adress', query: { userId: self.$route.query.userId }})
            }
           })
          }
          }
         }
     }
}
</script>

<style lang="less" scoped>
  .container{
      height: 100%;
      width:100%;
      background: #f6f6f6;
  }
  .inpbox{
      background: #fff;
  }
  .address-bom{
    width:80%;
    margin: 100px auto;
    border-radius: 100px;
    height:78px;
    align-items:center;
    background: #feba4e;
    line-height: 78px;
    font-size: 36px;
    color: #fff;
    justify-content: center;
    display: flex;
    z-index: 999;
  }
</style>
